package com.markus.code.top100;

import netscape.security.UserTarget;

/**
 * Author:markusZhang
 * Date:Create in 2020/8/19 18:54
 * todo: 搜索旋转排序数组
 */
public class LeetCode_33 {
    public int search(int[] nums, int target) {
        if (nums == null || nums.length == 0){
            return -1;
        }
        int low = 0;
        int high = nums.length-1;
        while(low <= high){
            int mid = low + ((high-low)>>1);
            if (target == nums[mid]){
                return mid;
            }
            //如果左边有序
            if (nums[low] <= nums[mid]){
                if (target>=nums[low] && target < nums[mid]){
                    high = mid-1;
                }else{
                    low = mid+1;
                }
            }else{
                if (target>nums[mid] && target<=nums[high]){
                    low = mid+1;
                }else{
                    high = mid-1;
                }
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int []nums = {3,1};
        int target = 1;
        LeetCode_33 demo = new LeetCode_33();
        System.out.println(demo.search(nums,target));
    }

}
